package com.pro.controller;

import com.google.gson.Gson;
import com.pro.pojo.JsComment;
import com.pro.pojo.JsGoodsComment;
import com.pro.pojo.JsProduct;
import com.pro.pojo.JsonData;
import com.pro.service.JsGoodsCommentService;
import com.pro.utils.RandomUtils;
import com.pro.utils.RedisCache;
import com.pro.utils.SensitiveWordsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:JsCommentController
 * Package:com.pro.controller
 * Description:
 *
 * @Date:2022/1/8 18:32
 * @Author: ZPT
 */
@RequestMapping("/comment")
@RestController
public class JsGoodsCommentController {
    @Autowired
    private JsGoodsCommentService jsCommentService;

    @Autowired
    private RedisCache redisCache;

    private static Gson gson;
    /**
     *
     * @author ZPT
     * @date 2022/1/8 18:26
     * @content :添加评论
     */
    @RequestMapping("/addComment")
    public JsonData addComment(JsComment comment){
        Boolean aBoolean = jsCommentService.addComment(comment);
        return aBoolean!=null? JsonData.buildSuccess(aBoolean): JsonData.buildError("服务端错误");
    }
    /**
     *
     * @author ZPT
     * @date 2022/1/8 18:42
     * @content :根据id查看评论
     */
    @RequestMapping("/getCommentById")
    public JsonData getCommentById(Integer id, Integer type){
        List<JsComment> commentList = jsCommentService.getCommentById(id, type);
        return commentList!=null? JsonData.buildSuccess(commentList): JsonData.buildError("服务端错误");
    }

    /**
     * @Description:
     * @Author: XJR
     * @Date: 2022/1/11
     * @Content:模糊查询各种状态的评论数量
     */
    @RequestMapping("/GoodsCountAllComment")
    public JsonData GoodsCountAllComment(String status,Integer type){
        status = !("").equals(status) && status !=null?"%"+status+"%":"%%";
        Integer integer = jsCommentService.GoodsCountAllComment(status,type);
        return integer!=null? JsonData.buildSuccess(integer): JsonData.buildError("服务端错误");
    };
    /**
     * @Description:
     * @Author: XJR
     * @Date: 2022/1/11
     * @Content:layui表格内容
     */

    @RequestMapping("/selectGoodsComment")
    public JsonData selectGoodsComment(String status, Integer limit, Integer page,Integer type){
        status = status!=null&&!status.equals("")?"%"+status+"%":"%%";
        List<JsComment> jsComments = jsCommentService.selectGoodsComment(status,limit,page,type);
        Integer integer = jsCommentService.GoodsCountAllComment(status,type);
        return jsComments!=null? JsonData.buildSuccess(jsComments,integer): JsonData.buildError("服务端错误");
    };
    /**
     * @Description:
     * @Author: XJR
     * @Date: 2022/1/11
     * @Content:表格内容
     */
    @RequestMapping("/selectGoodsNumber")
    public JsonData selectGoodsNumber(String status, Integer type,String commentDate){
        status = status!=null&&!status.equals("")?"%"+status+"%":"%%";
        Integer integer1 = jsCommentService.GoodsCountAllComment(status, type);
        Integer integer2 = jsCommentService.countHistoricalCommentNum(status,type);
        Integer integer3 = jsCommentService.countCommentsToday(status, type,commentDate);
        Integer integer4 = jsCommentService.countCommentPeople(status, type,commentDate);

        if(integer1==null||integer1.equals("")){
            integer1=0;
        }else if(integer2==null||integer2.equals("")){
            integer2=0;
        }else if (integer3==null||integer3.equals("")){
            integer3=0;
        }else if(integer4==null||integer4.equals("")){
            integer4=0;
        }
        JsGoodsComment comment = new JsGoodsComment();
        comment.setGoodsCountAllComment(integer1);
        comment.setCountHistoricalCommentNum(integer2);
        comment.setCountCommentsToday(integer3);
        comment.setCountCommentPeople(integer3);
        return comment!=null? JsonData.buildSuccess(comment): JsonData.buildError("服务端错误");
    }
    /**
     * @Description:
     * @Author: XJR
     * @Date: 2022/1/12
     * @Content:模糊查询各种状态的评论数量
     */
    @RequestMapping("/UserGoodsCountAllComment")
    public JsonData UserGoodsCountAllComment(String status,Integer type){
        status = !("").equals(status) && status !=null?"%"+status+"%":"%%";
        Integer integer = jsCommentService.UserGoodsCountAllComment(status,type);
        return integer!=null? JsonData.buildSuccess(integer): JsonData.buildError("服务端错误");
    };
    /**
     * @Description:
     * @Author: XJR
     * @Date: 2022/1/12
     * @Content:layui表格内容
     */
    @RequestMapping("/UserSelectGoodsComment")
    public JsonData UserSelectGoodsComment(String status, Integer limit, Integer page,Integer type){
        status = status!=null&&!status.equals("")?"%"+status+"%":"%%";
        List<JsComment> jsComments = jsCommentService.UserSelectGoodsComment(status,limit,page,type);
        Integer integer = jsCommentService.UserGoodsCountAllComment(status,type);
        return jsComments!=null? JsonData.buildSuccess(jsComments,integer): JsonData.buildError("服务端错误");
    };

    /**
     * @Description:
     * @Author: XJR
     * @Date: 2022/1/12
     * @Content:table表格内容
     */
    @RequestMapping("/UserSelectAll")
    public JsonData UserSelectAll(String status, Integer type){
        status = status!=null&&!status.equals("")?"%"+status+"%":"%%";
        Integer integer1 = jsCommentService.UserGoodsCountAllComment(status, type);
        Integer integer2 = jsCommentService.UserCountHistoricalCommentNum(status,type);
        Integer integer3 = jsCommentService.UserCountCommentsToday(status,type);
        Integer integer4 = jsCommentService.UserCountCommentPeople(status,type);

        if(integer1==null||integer1.equals("")){
            integer1=0;
        }else if(integer2==null||integer2.equals("")){
            integer2=0;
        }else if (integer3==null||integer3.equals("")){
            integer3=0;
        }else if(integer4==null||integer4.equals("")){
            integer4=0;
        }
        JsGoodsComment comment = new JsGoodsComment();
        comment.setGoodsCountAllComment(integer1);
        comment.setCountHistoricalCommentNum(integer2);
        comment.setCountCommentsToday(integer3);
        comment.setCountCommentPeople(integer4);
        return comment!=null? JsonData.buildSuccess(comment): JsonData.buildError("服务端错误");
    };
    /**
     * @Description:
     * @Author: XJR
     * @Date: 2022/1/12
     * @Content:查看评论详情
     */
    @RequestMapping("/selectGoodsComments")
    public JsonData selectGoodsComments(Integer id, Integer type){
        JsProduct jsProduct = jsCommentService.selectGoodsComments(id,type);
        return jsProduct!=null? JsonData.buildSuccess(jsProduct): JsonData.buildError("服务端错误");
    };

    /**
     * @Description:
     * @Author: XJR
     * @Date: 2022/1/12
     * @Content:删除评论（假删）
     */
    @RequestMapping("/delGoodsComment")
    public JsonData delGoodsComment(Integer id,Integer status){
        Boolean delboolean = jsCommentService.delGoodsComment(id,status);
        return delboolean!=null? JsonData.buildSuccess(delboolean): JsonData.buildError("服务端错误");
    };

    /**
     * @Description:
     * @Author: XJR
     * @Date: 2022/1/14
     * @Content:通过商品id获取其前台评论
     */
    @RequestMapping("/getBeforeCommentById")
    public JsonData getBeforeCommentById(Integer productId,Integer type){
        List<JsComment> beforeCommentById = jsCommentService.getBeforeCommentById(productId,type);
        List<JsComment> listFilter = SensitiveWordsUtil.getListFilter(beforeCommentById);
        return listFilter!=null? JsonData.buildSuccess(listFilter): JsonData.buildError("服务端错误");
    };

    /**
     * @Description:
     * @Author: XJR
     * @Date: 2022/1/14
     * @Content:添加商品评论
     */
    @RequestMapping("/addGoodsComment")
    public JsonData addGoodsComment(JsComment jsComment){
        System.out.println(jsComment);
        Boolean addBoolean = jsCommentService.addGoodsComment(jsComment);
        return addBoolean!=null? JsonData.buildSuccess(addBoolean): JsonData.buildError("服务端错误");
    };

    /**
     * @Description:
     * @Author: XJR
     * @Date: 2022/1/13
     * @Content:查看后台商品评论详情
     */
    @RequestMapping("/selectGoodsUserComments")
    public JsonData selectGoodsUserComments(Integer id, Integer type){
        JsProduct jsProduct = jsCommentService.selectGoodsUserComments(id, type);
        return jsProduct!=null? JsonData.buildSuccess(jsProduct): JsonData.buildError("服务端错误");
    };

    /**
     * @Description:
     * @Author: XJR
     * @Date: 2022/1/14
     * @Content:通过商品id获取其前台评论
     */
    @RequestMapping("/getUserCommentById")
    public JsonData getUserCommentById(Integer productId,Integer type){
        List<JsComment> userCommentById = jsCommentService.getUserCommentById(productId, type);
        List<JsComment> listFilter = SensitiveWordsUtil.getListFilter(userCommentById);
        return listFilter!=null? JsonData.buildSuccess(listFilter): JsonData.buildError("服务端错误");
    };
}
